package com.weetop.cfw.log_in

import android.os.Bundle
import android.os.Handler
import android.text.InputType
import android.view.View
import android.widget.Toast
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.AppUtils
import com.blankj.utilcode.util.EncryptUtils
import com.weetop.cfw.MainActivity
import com.weetop.cfw.R
import com.weetop.cfw.base.presenter.imp.PasswordLogInPresenterImp
import com.weetop.cfw.base.ui.activity.CommonBaseActivity
import com.weetop.cfw.base.view.PasswordLogInView
import com.weetop.cfw.bean.LogInBean
import com.weetop.cfw.constants.UrlConstants
import com.weetop.cfw.event.UpdateUserInfoEvent
import com.weetop.cfw.other.CommonWebViewActivity
import com.weetop.cfw.register.ForgetPasswordActivity
import com.weetop.cfw.register.RegisterActivity
import com.weetop.cfw.utils.EventBusUtils
import com.weetop.cfw.utils.MMKVUtils
import kotlinx.android.synthetic.main.activity_password_log_in.*
import kotlinx.android.synthetic.main.activity_password_log_in.editUserName
import kotlinx.android.synthetic.main.activity_password_log_in.select
import kotlinx.android.synthetic.main.activity_password_log_in.textPrivacyPolicy
import kotlinx.android.synthetic.main.activity_password_log_in.textUseProtocol
import kotlinx.android.synthetic.main.activity_register.*


/**
 * 密码登录界面
 */
class PasswordLogInActivity : CommonBaseActivity(), View.OnClickListener, PasswordLogInView {

    private lateinit var passwordLogInPresenterImp: PasswordLogInPresenterImp

    private var isPassword = true
    private var IsSelect: Int = 0//用户来源

    override fun getLayoutId() = R.layout.activity_password_log_in

    override fun initView(savedInstanceState: Bundle?) {
        passwordLogInPresenterImp = PasswordLogInPresenterImp()
        passwordLogInPresenterImp.attachView(this)

        setViewsOnClickListener(
            this,
            textForgetPassword,
            textRegister,
            textAtOnceLogIn,
            linearPasswordLogInBack,
            textUseProtocol,
            textPrivacyPolicy,
            imageShowOrHidePassword,
        select
        )
    }

    override fun initData(savedInstanceState: Bundle?) {

    }

    override fun onClick(p0: View?) {
        when (p0?.id) {
            R.id.textForgetPassword -> ActivityUtils.startActivity(ForgetPasswordActivity::class.java)
            R.id.textRegister -> ActivityUtils.startActivity(RegisterActivity::class.java)
            R.id.textAtOnceLogIn -> {
                if (IsSelect == 0) {
                    Toast.makeText(context, "请仔细阅读并同意用户协议", Toast.LENGTH_SHORT).show()
                    return
                }
                passwordLogInPresenterImp.passwordLogIn(
                    this@PasswordLogInActivity,
                    editUserName.text.toString().trim(),
                    EncryptUtils.encryptMD5ToString(editPassword.text.toString().trim())
                        .toLowerCase()
                )
            }
            R.id.linearPasswordLogInBack -> ActivityUtils.finishActivity(this)
            R.id.textUseProtocol -> CommonWebViewActivity.startCommonWebViewActivity(
                this,
                UrlConstants.use_protocol_web_url
            )
            R.id.textPrivacyPolicy -> CommonWebViewActivity.startCommonWebViewActivity(
                this,
                UrlConstants.privacy_policy_web_url
            )
            R.id.imageShowOrHidePassword -> {
                if (isPassword) {
                    editPassword.inputType = InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD
                    isPassword = false
                    imageShowOrHidePassword.setImageResource(R.mipmap.password_hide_logo)
                } else {
                    editPassword.inputType =
                        InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_PASSWORD
                    isPassword = true
                    imageShowOrHidePassword.setImageResource(R.mipmap.password_show_logo)
                }
            }
            R.id.select -> {
                if (IsSelect == 0) {
                    IsSelect = 1
                    select.setImageResource(R.mipmap.select_t);
                } else {
                    IsSelect = 0
                    select.setImageResource(R.mipmap.select_f);
                }
            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        passwordLogInPresenterImp.detachView()
    }

    /**
     * 登录成功
     */
    override fun logInSuccess(logInBean: LogInBean?) {
        if (logInBean != null) {
            showNativeShortToast("登录成功")
            MMKVUtils.saveToken(logInBean.token)
            MMKVUtils.saveUlevel(logInBean.ulevel)
            EventBusUtils.post(logInBean)
            EventBusUtils.post(UpdateUserInfoEvent())
            ActivityUtils.finishActivity(this)
//            ActivityUtils.startActivity(MainActivity::class.java)
        }
    }
}
